from lxml import html

from news_2.src.main.utils.file_tool import read_file_contents

"""
    所需数据 ：
        1.html元素对象的value中
        2.html元素对象的属性中 属性名
    {
        x_path:'//*[@id="tdi_69"]/div/div/div[1]/h2/a'
        is_attr:'href'

    }
"""

title = dict(key='title', x_path='//*[@id="tdi_69"]/div/div/div[1]/h2/a', is_attr='')

link = dict(key='link', x_path='//*[@id="tdi_69"]/div/div/div[1]/h2/a', is_attr='href')

conf = {
    'url': "https://yktnews.com",
    'html_tag': [title, link],
    'encode': 'iso-8859-1',
    'decode': 'utf-8',
}


class HtmlTagPath:
    def __init__(self, key, x_path, attr):
        self.key = key
        self.path = x_path
        self.attr = attr
        self.element = None
        self.data_value = None

    """
        是否为提取属性数据值
    """

    def __is_attr(self):
        if self.attr is not None and self.attr != "":
            return True
        else:

            return False

    """
        设置标签元素
    """

    def set_element(self, html_content):

        if self.path is not None or not self.path != "":
            tree = html.fromstring(html_content)
            element = tree.xpath(self.path)
            self.element = element

    """
        根据标签元素提取数据
    """

    def process_data_value(self):
        if self.element is None or len(self.element) < 1:
            return
        if self.__is_attr():
            self.data_value = self.element[0].get(self.attr)
        else:
            self.data_value = self.element[0].text_content()

    """
        返回数据
    """

    def get_data_value(self):
        return self.data_value


class LxmlExtractNews:
    def __init__(self, url='', encode='', decode='utf-8'):
        self.url = url
        self.encode = encode
        self.decode = decode

        self.html_content = ""
        self.tags = []
        self.data = []

        #
        # self.build_html_content()
        #

    # 是否需要解码
    def __is_decode(self):
        if self.encode is not None and self.encode != "":
            return True
        else:
            return False

    def add_tags(self, tag_obj):
        self.tags.append(tag_obj)

    """
        发起请求获取 html文本
    """

    def build_html_content(self):
        # return requests.get(self.url).text
        self.html_content = read_file_contents("../resources/yktnews.txt")

    """
        解码 html文本
    """

    def decode_html(self):
        print("解码")
        if self.html_content is not None and self.html_content != "" and self.__is_decode():
            self.html_content = self.html_content.encode(self.encode).decode(self.decode)

    """
        转换path数据为对象
    """

    def build_html_tags(self, tag_path=None):
        print("构建加工对象")
        if tag_path is None or not isinstance(tag_path, list):
            tag_path = []
        for tag in tag_path:
            tag_obj = HtmlTagPath(key=tag['key'], x_path=tag['x_path'], attr=tag['is_attr'])
            self.add_tags(tag_obj)

    """
        使用HtmlTag对象获取数据
    """

    def process_html_tag_data(self):
        print("调用加工对象加工数据")
        for tag in self.tags:
            # 1.配置标签
            tag.set_element(self.html_content)
            # 2.提取数据
            tag.process_data_value()


if __name__ == '__main__':
    news = LxmlExtractNews(conf['url'], conf['encode'], conf['decode'])
    news.build_html_content()
    news.decode_html()
    news.build_html_tags(conf['html_tag'])
    news.process_html_tag_data()
    for item in news.tags:
        print(f"{item.key}:{item.get_data_value()}")
